package com.example.androidmainshixun.view;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.androidmainshixun.R;
import com.example.androidmainshixun.database.DBHelper;
import com.example.androidmainshixun.entity.User;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    public static final int RESULT_CODE_REGISTER = 0;
    private Button btnRegister;
    private EditText etAccount,etPass,etPassConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        etAccount = findViewById(R.id.et_account);
        etPass = findViewById(R.id.et_password);
        etPassConfirm = findViewById(R.id.et_password_confirm);
        btnRegister = findViewById(R.id.btn_register);
        btnRegister.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        String username = etAccount.getText().toString();
        String pass = etPass.getText().toString();
        String passConfirm = etPassConfirm.getText().toString();
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(RegisterActivity.this, "手机号不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        if (TextUtils.isEmpty(pass)) {
            Toast.makeText(RegisterActivity.this, "密码不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        if (!TextUtils.equals(pass,passConfirm)) {
            Toast.makeText(RegisterActivity.this, "密码不一致", Toast.LENGTH_LONG).show();
            return;
        }
        if (allInputNotEmpty(username,pass,passConfirm)){
            User user=new User();
            user.setUsername(username);
            user.setPass(pass);
            int id= (int)  DBHelper.getInstance(RegisterActivity.this).insertuser(user);
            if(id>=0){
                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 1.5秒后执行的代码：跳转到LoginActivity
                        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                        startActivity(intent);
                        finish(); // 结束当前的RegisterActivity
                    }
                }, 1500);
                etAccount.setText("");
                etPass.setText("");
            }

        }
        // 存储注册的用户名 密码
        SharedPreferences spf = getSharedPreferences("spfRecord", MODE_PRIVATE);
        SharedPreferences.Editor edit = spf.edit();
        edit.putString("account", username);
        edit.putString("password", pass);
        edit.apply();
        // 数据回传
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("account",username);
        bundle.putString("password",pass);
        intent.putExtras(bundle);
        setResult(RESULT_CODE_REGISTER,intent);
        this.finish();
    }
    private boolean allInputNotEmpty(String... inputStrings) {
        for (int i = 0; i < inputStrings.length; i++) {
            if (TextUtils.isEmpty(inputStrings[i])) {
                return false;
            }
        }
        return true;
    }
}